package com.wx.aspect;


import com.wx.controller.prediction.ErrorController;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Aspect
@Component
public class HttpAspect {

    private final  static Logger logger= LoggerFactory.getLogger(HttpAspect.class);

    @Pointcut("execution(public * com.wx.controller.verifyController.VerifyController.*(..))")
//    @Pointcut("execution(public * com.wx.controller.*.*.*(..))")
    public void point(){
    }

    @Before("point()")
    public void doBefore(JoinPoint joinPoint) throws IOException {
        logger.info("brfore");
        ServletRequestAttributes attributes= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request= attributes.getRequest();
        HttpServletResponse response=attributes.getResponse();

        //url
        logger.info("url={}", request.getRequestURL());
        //访问前缀
        logger.info("请求地址{}",request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort());
        //session
        logger.info("seesion={}", request.getSession().getAttribute("username"));
        //method
        logger.info("method={}", request.getMethod());
        //session
        logger.info("seesion={}", request.getSession().getAttribute("username"));
        //ip
        logger.info("ip={}", request.getRemoteAddr() );
        //类方法
        logger.info("类方法={}", joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName());
        //参数
        logger.info("参数={}", joinPoint.getArgs());
//        http://localhost:8080/user/index
//        这个是外部跳转，也叫重定向。请求不带参数，重新连接一个url
        if (request.getSession().getAttribute("username")==null){
            System.out.println(request.getSession().getAttribute("username") );
       response .sendRedirect(request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/user/login");
        }

    }

//    @AfterReturning(pointcut = "point()",returning = "object")
//    public  void doAfterReturn(Object object){
//        logger.info("response={}",object.toString());
//    }


}
